import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:ty_smart_watch/page/me/me_page.dart';
import 'package:ty_smart_watch/page/smartlife/smart_life_page.dart';
import 'package:ty_smart_watch/page/sport/sport_health_page.dart';
import 'package:ty_smart_watch/utils/Log.dart';

///底部导航栏制做，
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _currentIndex = 0;
  final _pageController = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.d("My Home Page");
    return Scaffold(
      // 沉浸式状态栏
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        toolbarHeight: 0,
      ),
      body: PageView(
        controller: _pageController,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        children: [
          SmartLifePage(onPageChanged: onPageChanged),
          const SportHealthPage(),
          const MePage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);

          setState(() {
            _currentIndex = index;
          });
        },
        items: bottomItems.map((data) {
          return BottomNavigationBarItem(
              icon: Image.asset(
                data.iconU,
                width: 24,
                height: 24,
              ),
              activeIcon: Image.asset(
                data.iconN,
                width: 24,
                height: 24,
              ),
              label: data.title);
        }).toList(),
        selectedItemColor: const Color(0xFF308DFC),
        unselectedItemColor: const Color(0xFF737A81),
      ),
    );
  }

  ///其他页面跳转回调函数
  void onPageChanged(int index) {
    _pageController.jumpToPage(index);
    setState(() {
      _currentIndex = index;
    });
  }
}

/// 封装导航栏的图标与文本数据
class TabData {
  /// 导航数据构造函数
  const TabData(
      {required this.index, required this.title, required this.iconN, required this.iconU});

  /// 导航标题
  final String title;

  /// 导航图标未选中
  final String iconU;

  /// 导航图标已选中
  final String iconN;

  /// 索引
  final int index;
}

/// 导航栏数据集合
const List<TabData> bottomItems = <TabData>[
  TabData(
      index: 0,
      title: '智能生活',
      iconU: "images/bottomBar/bottom_smart_life_icon_u.png",
      iconN: "images/bottomBar/bottom_smart_life_icon_n.png"),
  TabData(
      index: 1,
      title: '运动健康',
      iconU: "images/bottomBar/bottom_sport_health_icon_u.png",
      iconN: "images/bottomBar/bottom_sport_health_icon_n.png"),
  TabData(
      index: 2,
      title: '我的',
      iconU: "images/bottomBar/bottom_me_icon_u.png",
      iconN: "images/bottomBar/bottom_me_icon_n.png"),
  // TabData(index: 1, title: '运动健康', icon: Icons.camera),
  // TabData(index: 2, title: '我的', icon: Icons.search),
];
